package com.yf.modules.security.controller;

import com.yf.Utils.SecurityUtils;
import com.yf.modules.security.model.dto.AuthUserDto;
import com.yf.modules.security.service.api.AuthService;
import com.yf.response.ResponseBody;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author chentianwei
 * @date 2023/5/5 9:02
 * @description 安全模块rest接口
 */
@RestController
@RequestMapping("/auth")
@Slf4j
@Tag(name = "SecurityAction",description = "安全模块操作")
public class AuthorizationController {

    private final AuthService authService;

    public AuthorizationController(AuthService authService) {

        this.authService = authService;
    }

    /**
     * 登录
     * @param authUserDto 用户信息
     */
    @PostMapping("/login")
    public ResponseEntity<ResponseBody<Object>> login(@RequestBody AuthUserDto authUserDto){
        Map<String, Object> map = authService.login(authUserDto);
        return ResponseEntity.ok().body(ResponseBody.success(map));
    }

    /**
     * 登出
     * @param request http请求
     */
    @DeleteMapping("/logout")
    public ResponseEntity<ResponseBody<Object>> logout(HttpServletRequest request){
        authService.logout(request);
        return ResponseEntity.ok().body(ResponseBody.success("退出成功"));
    }

    /**
     * 获取UserDetail明细
     * @return 用户明细
     */
    @GetMapping("/info")
    public ResponseEntity<ResponseBody<Object>> getInfo(){
        return ResponseEntity.ok().body(ResponseBody.success(SecurityUtils.getCurrentUser()));
    }
}
